///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2007-2009 ILOG, S.A.
//  All Rights Reserved.
//  The following is ILOG Source Code.  No modifications may be made to the  
//  ILOG Source Code.  Any usage of the ILOG Source Code is subject to 
//  the terms and conditions of the ILOG End User License Agreement   
//  applicable to this ILOG software product.
//
///////////////////////////////////////////////////////////////////////////////

package ilog.gauges.circular.renderers
{
  import flash.display.Graphics;
  import flash.geom.Point;
  import flash.geom.Rectangle;
  
  import ilog.gauges.gaugesClasses.circular.CircularGaugeElement;
  import ilog.utils.CSSUtil;
  
  import mx.graphics.GradientEntry;
  import mx.graphics.IFill;
  import mx.graphics.RadialGradient;
  import mx.graphics.Stroke;
  import mx.styles.CSSStyleDeclaration;

  /**
   * Specifies the stroke for the renderer.
   * This style must be an instance of an implementation of the <code>IStroke</code> interface.
   * 
   * @default undefined 
   */
  [Style(name="stroke", type="mx.graphics.IStroke", inherit="no")]

  /**
   * Specifies the fill for the renderer.
   * This style must be an instance of an implementation of the <code>IFill</code> interface.
   * 
   * The default value is a radial gradient.
   * 
   *  
   */
  [Style(name="fill", type="mx.graphics.IFill", inherit="no")]

  /**
   * A basic circle renderer to use in circular gauges. You can customize the fill and stroke of this
   * visual element. Inherited radius and origin properties are used for positioning.
   * It can be used for needle caps or gauge backgrounds.  
   * 
   * @mxml
   *  
   * <p>The <code>&lt;ilog:CircleRenderer&gt;</code> tag inherits all the properties
   * of its parent classes and adds the following properties:</p>
   *  
   * <pre>
   * &lt;ilog:CircleRenderer
   *   <strong>Styles</strong>
   *   fill="IFill; a grey-style radial gradient"
   *   stroke="IStroke; no default"
   * /&gt;
   * </pre>
   * 
   * @includeExample examples/CircleRendererExample.mxml -noswf 
   */ 
  public class CircleRenderer extends CircularGaugeElement
  {
    
    /**
     *  Constructor
     */    
    public function CircleRenderer()
    {
      super();
    }
    
    /**
     *  @private
     */
    private static var stylesInited:Boolean = initStyles(); 

    /**
     *  @private
     */
    private static function initStyles():Boolean
    {
      var pStyle:CSSStyleDeclaration =
        CSSUtil.createSelector("CircleRenderer");   
            
      pStyle.defaultFactory = function():void
        {
          var fill:RadialGradient = new RadialGradient();
          var g1:GradientEntry = new GradientEntry(0xEEEEEE, 0.0, 1);
          var g2:GradientEntry = new GradientEntry(0xCCCCCC, 0.8, 1);
          fill.entries = [g1, g2 ];
          this.fill = fill;
          
        }

      return true;
    }
    
    /**
     * Draws the circle.
     *
     * @param unscaledWidth Specifies the width, in pixels, of the component
     * in the component's coordinates, regardless of the value of the
     * <code>scaleX</code> property of the component.
     *
     * @param unscaledHeight Specifies the height, in pixels, of the component
     * in the component's coordinates, regardless of the value of the
     * <code>scaleY</code> property of the component.   
     */    
    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
      super.updateDisplayList(unscaledWidth, unscaledHeight);
      var g:Graphics = graphics;
      var o:Point = getOrigin();
      var r:Number = getRadius();
      var fill:IFill = CSSUtil.fillFromStyle(getStyle("fill"));
      var stroke:Stroke = getStyle("stroke");

      g.clear();

      if (stroke)
        stroke.apply(g);
      
      if (fill)
      {
        fill.begin(g, new Rectangle(o.x-r, o.y-r, 2*r, 2*r));        
      }
      g.drawCircle(o.x, o.y, r);
      
      if (fill)
        fill.end(g);
    }
  }
}
